package com.cnhis.cloudhealth.clinical.clidoctor.clinicaldoctoradviceeditor.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.clinicaldoctoradviceeditor.dao.DoctorAdviceEditorDao;
import com.cnhis.cloudhealth.clinical.clidoctor.clinicaldoctoradviceeditor.mapper.CliVao1;
import com.cnhis.cloudhealth.clinical.clidoctor.cliparamter.dao.CliParamterDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class DoctorAdviceEditorBo
{
    @Resource
    private DoctorAdviceEditorDao adviceEditorDao;
    
    @Resource
    private CliParamterDao cliParamterDao;
    
    Logger log = Logger.getLogger(DoctorAdviceEditorBo.class);
    
    public ModelVo queryItemExecuteOffice(int currentPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = adviceEditorDao.queryItemExecuteOffice(currentPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo insertByVo(CliVao1 vo)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            adviceEditorDao.insertByVo(vo);
        }
        catch (Exception e)
        {
            
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo storageDoctorAdviceRecord()
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = adviceEditorDao.storageDoctorAdviceRecord();
            
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    public ModelVo queryClinicalPatientinformation(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = adviceEditorDao.queryClinicalPatientinformation(map);
            
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo storageNewDiagnosisRecord(Map map)
        throws Exception
    {
        
        ModelVo mo = new ModelVo();
        List list = new ArrayList<>();
        try
        {
            
            String value = cliParamterDao.getSysParamValue(map);
            map.put("parav", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            if (value != null && !"".equals(value))
            {
                if ("1".equals(value) && !map.get("lVAF01").toString().equals(map.get("lVAF01A").toString()))
                {
                    map.put("lVAF01A", -999);
                }
            }
            list = adviceEditorDao.storageNewDiagnosisOutRecord(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo personalOftenList(Map map, int currentPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
//        Page pg = null;
        try
        {
            
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            List list = adviceEditorDao.personalOftenList(map);
//            pg = adviceEditorDao.personalOftenList(map, currentPage, pageSize);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    public ModelVo personalTherapeuticRegimen(Map map, int currentPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = adviceEditorDao.personalTherapeuticRegimen(map, currentPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo requestNote(Map map, int currentPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = adviceEditorDao.requestNote(map);
            Set<String> set = new HashSet<String>();
            for (int i = 0; i < list.size(); i++)
            {
                Map temp = (Map)list.get(i);
                set.add(temp.get("bdo03").toString());
            }
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setRows(new ArrayList<>(set));
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    @SuppressWarnings("rawtypes")
    public ModelVo requestNoteDetailItem(Map map, int currentPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = adviceEditorDao.requestNoteDetailItem(map);
            Set set = new HashSet<>();
            for (int i = 0; i < list.size(); i++) {
            	Map temp = (Map) list.get(i);
				set.add(temp.get("bdo03"));
			}
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setRows(new ArrayList<>(set));
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }
    
    @SuppressWarnings("rawtypes")
    public ModelVo rapeuticSchedule(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = adviceEditorDao.rapeuticSchedule(map);
            List bdo1List = adviceEditorDao.bdo1List(map);
            mo.setList(bdo1List);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }
    
    public ModelVo treeList(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = adviceEditorDao.treeList(map);
            // 构造树形结构数据
            Set<Integer> set = new HashSet<Integer>();
            List<Map<String, Object>> treeRoot = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> temp : (List<Map<String, Object>>)list)
            {
                if (!set.contains(temp.get("bdo01").toString()))
                {
                    // 根节点
                    set.add(Integer.parseInt(temp.get("bdo01").toString()));
                }
            }
            for (Map<String, Object> temp : (List<Map<String, Object>>)list)
            {
                // 根节点
                Object parentid = temp.get("patientid");
                if (parentid!=null && !set.contains(Integer.parseInt(parentid.toString())))
//                if (parentid==null)
                {
                    // 子节点(递归)
                    configTreeNode(list, temp);
                    treeRoot.add(temp);
                }
            }
            
           /* List<Map<String, Object>> finalList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> quanyuanList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> keshiList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> gerenList = new ArrayList<Map<String, Object>>();
            for(Map<String, Object> temp1 : treeRoot)
            {
                if("2".equals(temp1.get("bdo10").toString()))
                {
                    quanyuanList.add(temp1);
                }
                else if("1".equals(temp1.get("bdo10").toString()))
                {
                    keshiList.add(temp1);
                }
                else if("0".equals(temp1.get("bdo10").toString()))
                {
                    gerenList.add(temp1);
                }
            }
            Map<String, Object> tempQ = new HashMap<String, Object>();
            tempQ.put("text", "全院方案");
            tempQ.put("nodes", quanyuanList);
            Map<String, Object> tempK = new HashMap<String, Object>();
            tempK.put("text", "科室方案");
            tempK.put("nodes", keshiList);
            Map<String, Object> tempG = new HashMap<String, Object>();
            tempG.put("text", "个人方案");
            tempG.put("nodes", gerenList);
            finalList.add(tempQ);
            finalList.add(tempK);
            finalList.add(tempG);*/
//            mo.setList(finalList);//全院、科室、个人分类
            mo.setList(treeRoot);//不分类
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    private void configTreeNode(List<Map<String, Object>> ls, Map<String, Object> temp)
    {
        List<Map<String, Object>> treeNode = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> temp1 : (List<Map<String, Object>>)ls)
        {
            if (temp1.get("patientid")!=null && temp1.get("patientid").equals(temp.get("bdo01")))
            {
                treeNode.add(temp1);
                configTreeNode(ls,temp1);
            }
        }
        temp.put("text", temp.get("bdo02"));
        temp.put("nodes", treeNode);
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo classificationRetrieval(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page page;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            Map<String, Object> temp = new HashMap<String, Object>();
            temp.put("productid", 9999);
            temp.put("programid", 9999);
            temp.put("paramno", 106);
            String value106 = cliParamterDao.getSysParamValue(temp);
            map.put("gparam60", value);
            map.put("value106", value106);
            page = adviceEditorDao.classificationRetrieval(map, currPage, pageSize);
            mo.setList(page.getList());
            mo.setTotal(page.getTotalNumber());
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    @SuppressWarnings({"rawtypes", "unchecked"})
    public ModelVo medTreeToDetail(Map map, int currPage, int pageSize)
        throws Exception
    {
        long t1 = System.currentTimeMillis();
        // 获取参数
        //Map params = getMttdParams();
        // 获取当前时间
        String lDate = DateUtils.getDateStr(new Date()) + " 23:59:59.999";
        // 获取检索规则
        //String aParamno = params.get("aparamno").toString();
        try{
        Integer lACF01 = Integer.parseInt(map.get("lACF01").toString());
        
        // 参数整合
        //map.putAll(params);
        HashMap<String, Object> temp1 = new HashMap<String,Object>();
        temp1.put("productid", 9999);
        temp1.put("programid", 9999);
        temp1.put("paramno", 60);
        map.put("gparam60", cliParamterDao.getSysParamValue(temp1));
        HashMap<String, Object> temp2 = new HashMap<String,Object>();
        temp2.put("productid", 9999);
        temp2.put("programid", 9999);
        temp2.put("paramno", 1);
        map.put("aparam1", cliParamterDao.getSysParamValue(temp2));
        HashMap<String, Object> temp3 = new HashMap<String,Object>();
        temp3.put("productid", 9999);
        temp3.put("programid", 9999);
        temp3.put("paramno", 15);
        String aParamno = cliParamterDao.getSysParamValue(temp3);
        map.put("aParamno",aParamno);
        //以后把上面的放到redis中，注释下面的代码
//        String aParamno = "0";
//        map.put("gparam60", MapUtils.getString(map,"gparam60","0"));
//        map.put("aparam1", MapUtils.getString(map,"aparam1","1"));//是否显示0库存的药（1为显示）
//        map.put("aParamno",MapUtils.getString(map,"aParamno","1"));
        
        map.put("lDate", lDate);
        Page page = null;
        /*if ("0".equals(aParamno))
        {
            // 按默认规则检查
            page = getaParamno0List(map, currPage, pageSize);
        }
        else
        {
            if ("3".equals(aParamno))
            {
                if (lACF01.equals(1))
                {
                    page = getaParamnoe1List(map, currPage, pageSize);
                }
            }
            else
            {
                page = getaParamnoeList(map, currPage, pageSize);
            }
        }*/
        page = getaParamno0List(map, currPage, pageSize);
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS, "");
        mo.setList(page.getList());
        mo.setTotal(page.getTotalNumber());
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
        return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            ModelVo mo = new ModelVo(StaticKeys.OH_FALSE, "");
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    private Page getaParamnoe2List(Map map, int currentPage, int pageSize)
        throws Exception
    {
        return cliParamterDao.getaParamnoe2List(map, currentPage, pageSize);
    }
    
    private Page getaParamnoe1List(Map map, int currPage, int pageSize)
        throws Exception
    {
        return cliParamterDao.getaParamnoe1List(map, currPage, pageSize);
    }
    
    private Page getaParamnoeList(Map map, int currPage, int pageSize)
        throws Exception
    {
        return cliParamterDao.getaParamnoeList(map, currPage, pageSize);
    }
    
    /**
     * 
     * @param map
     * @return
     * @throws Exception
     */
    private Page getaParamno0List(Map map, int currPage, int pageSize)
        throws Exception
    {
        return cliParamterDao.getaParamno0List(map, currPage, pageSize);
    }
    
    private Map getMttdParams()
        throws Exception
    {
        return cliParamterDao.getMttdParams();
    }
    
    public ModelVo checkDrugstore(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            if (value != null && !"".equals(value))
            {
                if ("0".equals(value))
                {
                    list = adviceEditorDao.checkDrugstore0(map);
                }
                else if ("1".equals(value))
                {
                    list = adviceEditorDao.checkDrugstore1(map);
                }
                else if ("2".equals(value) || "3".equals(value))
                {
                    list = adviceEditorDao.checkDrugstore2or3(map);
                }
                
            }
            
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo queryFeeDetail(Map map)
        throws Exception
    {
        long l1 = System.currentTimeMillis();
        ModelVo mo = new ModelVo();
        String val = null;
        try
        {
            if ("1".equals(map.get("ACF01").toString()))
            {
                val = adviceEditorDao.getABC02(map);
            }
            else
            {
                val = adviceEditorDao.getABC021(map);
            }
            map.put("ABC02", val == null ? "" : val);
            List list = adviceEditorDao.queryFeeDetail(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            long l2 = System.currentTimeMillis();
            log.info("-------------------检验智慧医保服务耗时"+(l2-l1)+"毫秒------------------");
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo queryMedicineAdviceFeeDetail(Map map)
        throws Exception
    {
        long l1 = System.currentTimeMillis();
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String aDate = map.get("aDate").toString() + " 23:59:59.999";
            map.put("aDate", aDate);
            String ABC02 = getABC02(map);
            map.put("ABC02", ABC02);
            List l = getParam(map);
            map.put("BBY08", 1);
            map.put("BAG057", 1);
            map.put("BBY25", 1);
            if (l != null && l.size() > 0)
            {
                Map m = (Map)l.get(0);
//                map.put("BBY08", m.get("BBY08").toString());
//                map.put("BAG057", m.get("BAG057").toString());
                map.put("BBY25", m.get("bby25").toString());
                
            }
            list = adviceEditorDao.queryMedicineAdviceFeeDetail(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        long l2 = System.currentTimeMillis();
        log.info("--------------------智慧医保药品数据服务耗时："+(l2-l1)+"毫秒---------------------");
        return mo;
    }
    
    private String getABC02(Map map)
        throws Exception
    {
        String abc02 = "";
        if ("1".equals(map.get("ACF01").toString()))
        {
            abc02 = adviceEditorDao.getABC02Param1(map);
        }
        else
        {
            abc02 = adviceEditorDao.getABC02Param2(map);
        }
        return abc02 == null ? "" : abc02;
    }
    
    private List getParam(Map map)
        throws Exception
    {
        return adviceEditorDao.paramters(map);
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo unSendAdviceValuationPrice(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String aDate = null;
            Object date = map.get("aDate");
            if (date != null)
            {
                if (!"".equals(date.toString()))
                {
                    aDate = date.toString() + " 23:59:59.999";
                }
            }
            else
            {
                aDate = DateUtils.getDateStr(new Date()) + " 23:59:59.999";
            }
            map.put("aDate", aDate);
            list = adviceEditorDao.unSendAdviceValuationPrice(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(list);
        return mo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo queryItemCondition(Map map, int currentPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        
        try
        {
            Object lDate = map.get("lDate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("lDate", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
            }
            HashMap hs = new HashMap();
            hs.put("productId", 9999);
            hs.put("programId", 9999);
            hs.put("paramNo", 60);
            String gparam60 = cliParamterDao.getSysParamValue(hs);
            map.put("gparam60", gparam60);
            pg = adviceEditorDao.queryItemCondition(map, currentPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo queryBBY1ByVAF01(HashMap map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            List<Map<String,Object>> list = adviceEditorDao.queryBBY1ByVAF01(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setList(list);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryVBQ1ByCBM01(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List<Map<String,Object>> list = adviceEditorDao.queryVBQ1ByCBM01(map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setList(list);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg(e.getMessage());
            return mo;
        }
    }

    public ModelVo personalOftenListXiyao(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            list = adviceEditorDao.personalOftenListXiyao(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo personalOftenListJiancha(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            list = adviceEditorDao.personalOftenListJiancha(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo personalOftenListJianyan(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            list = adviceEditorDao.personalOftenListJianyan(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo personalOftenListShoushu(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            list = adviceEditorDao.personalOftenListShoushu(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo personalOftenListZhiliao(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            list = adviceEditorDao.personalOftenListZhiliao(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }


    public ModelVo personalOftenListZhongyao(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            list = adviceEditorDao.personalOftenListZhongyao(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    @SuppressWarnings("rawtypes")
    public ModelVo queryYangben(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = adviceEditorDao.queryYangben(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
    }

    @SuppressWarnings("rawtypes")
    public ModelVo checkdrugstoreNew(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            if (value != null && !"".equals(value))
            {
                if ("0".equals(value))
                {
                    list = adviceEditorDao.checkDrugstore0New(map);
                }
                else if ("1".equals(value))
                {
                    list = adviceEditorDao.checkDrugstore1New(map);
                }
                else if ("2".equals(value) || "3".equals(value))
                {
                    list = adviceEditorDao.checkDrugstore2or3New(map);
                }
            }
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo getVaf1Byvaf01a(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = adviceEditorDao.getVaf1Byvaf01a(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo classificationretrieval_fangan(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("gparam60", value);
            List list = adviceEditorDao.classificationretrieval_fangan(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo jiansuoFangan(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            map.put("productid", 9999);
            map.put("programid", 9999);
            map.put("paramno", 60);
            String value = cliParamterDao.getSysParamValue(map);
            map.put("gparam60", value);
            List list = adviceEditorDao.jiansuoFangan(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo treeList1(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = adviceEditorDao.treeList1(map);
            // 构造树形结构数据
            Set<Integer> set = new HashSet<Integer>();
            List<Map<String, Object>> treeRoot = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> temp : (List<Map<String, Object>>)list)
            {
                // 根节点
                set.add(Integer.parseInt(temp.get("bdo01").toString()));
            }
            for (Map<String, Object> temp : (List<Map<String, Object>>)list)
            {
                // 根节点
                Object parentid = temp.get("patientid");
                if (parentid!=null && !set.contains(Integer.parseInt(parentid.toString())))
                {
                    // 子节点(递归)
                    configTreeNode(list, temp);
                    treeRoot.add(temp);
                }
            }
            
            
            List<Map<String, Object>> finalList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> quanyuanList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> keshiList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> gerenList = new ArrayList<Map<String, Object>>();
            for(Map<String, Object> temp1 : treeRoot)
            {
                if("2".equals(temp1.get("bdo10").toString()))
                {
                    quanyuanList.add(temp1);
                }
                else if("1".equals(temp1.get("bdo10").toString()))
                {
                    keshiList.add(temp1);
                }
                else if("0".equals(temp1.get("bdo10").toString()))
                {
                    gerenList.add(temp1);
                }
            }
            Map<String, Object> tempQ = new HashMap<String, Object>();
            tempQ.put("text", "全院方案");
            tempQ.put("nodes", quanyuanList);
            Map<String, Object> tempK = new HashMap<String, Object>();
            tempK.put("text", "科室方案");
            tempK.put("nodes", keshiList);
            Map<String, Object> tempG = new HashMap<String, Object>();
            tempG.put("text", "个人方案");
            tempG.put("nodes", gerenList);
            finalList.add(tempQ);
            finalList.add(tempK);
            finalList.add(tempG);
//            mo.setList(finalList);//全院、科室、个人分类
            mo.setList(treeRoot);//不分类
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo storageNewDiagnosisRecord1(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = new ArrayList<>();
        try
        {
            
            String value = cliParamterDao.getSysParamValue(map);
            map.put("parav", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            if (value != null && !"".equals(value))
            {
                if ("1".equals(value) && !map.get("lVAF01").toString().equals(map.get("lVAF01A").toString()))
                {
                    map.put("lVAF01A", -999);
                }
            }
            list = adviceEditorDao.storageNewDiagnosisOutRecord1(map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo personalOftenListCailiao(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            map.put("aParamno", value == null || "".equals(value) ? 0 : Integer.parseInt(value));
            list = adviceEditorDao.personalOftenListCailiao(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }

    public ModelVo checkdrugstoreNew_cailiao(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            String value = cliParamterDao.getSysParamValue(map);
            if ("0".equals(value))
            {
                list = adviceEditorDao.checkdrugstoreNew_cailiao(map);
            }
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
}
